/* 2013/06/30 author:mike */
/* *
 * 首页添加商品到购物车 
 */
function init_addtocart(goodsId, parentId)
{
	var goods        	= new Object();
	//var spec_arr     = 0;
	var spec_arr 		= new Array();
	var number 		= document.getElementById(goodsId).value;
	var quick		  	= 1;
	
	var attr_id = document.getElementById('goods_attr_id_' + goodsId).value;
	
	if(isNaN(number) || isNaN(attr_id) || number.length > 3 || attr_id.length > 5 ){
		// 数值有问题，给个默认属性
		spec_arr = 0;				
	}else{
		spec_arr['0'] 	= attr_id;
	}

	spec_arr['0'] 	= attr_id;
	goods.quick    	= quick;
	goods.spec 		= spec_arr;	
	
	goods.goods_id = goodsId;
	goods.number   = number;
	goods.parent   = (typeof(parentId) == "undefined") ? 0 : parseInt(parentId);
	
	Ajax.call('flow.php?step=add_to_cart', 'goods=' + $.toJSON(goods), init_addtocart_Response, 'POST', 'JSON');
}

/* *
 * 处理添加商品到购物车的反馈信息
 */
function init_addtocart_Response(result)
{
	if (result.error > 0)
	{
		$("#tips_big_div").show();
		$("#tips_word").html("添加失败。");
	}
	else
	{
		$("#ECS_CART").html(result.cart_goods_list);
		$("[name=shopping_Car]").show();
		$("[name=shopping_Car]").delay(5000).hide(0); 
	}
}

// sshi 过滤字符串中的非数字元素，小于1的返回1
function intval_filter(goods_number){
	if( isNaN(goods_number)){
		goods_number ='1';
	}
	goods_number = Math.ceil(goods_number);
	if( eval(goods_number) < 1){
		goods_number = '1';	
	}	
	return goods_number;
}


function del(goods_id){
	var num = document.getElementById(goods_id);
	var n = parseInt(num.value);
	n = intval_filter(n);
	num.value = n;
	if(n-1<=0){
		return false;
	}else{
	num.value = n-1;
	}
}
function add(goods_id){
	var num = document.getElementById(goods_id);
	var n = parseInt(num.value);
	n = intval_filter(n);	
	num.value = eval(n) + 1;
}

function delete_cart(rec_id){
	Ajax.call('flow.php?step=delete_cart_goods', 'id=' + rec_id , delete_cartResponse, "POST", "JSON");
}
function delete_cartResponse(result)
{
	if (result.error = 0)
	{
		$("#tips_big_div").show();
		$("#tips_word").html("删除失败。");
	}
	else
	{		
		$("#ECS_CART").html(result.cart_goods_list);
		$("[name=shopping_Car]").show();
		// 如果当前页面是购物车，则刷新购物车
		var path = window.location.pathname;
		if(path.indexOf('flow.php') == 1 ){
			window.location.href='flow.php?step=cart';	
		}
	}
}

$(function(){
//找回密码
$("[name=find_username]").blur(function(){ 								
	$mobile = $.trim($("[name=find_username]").val());
	if(isMobile_Email($mobile) == false){
		$("#find_show").html("请输入正确的手机号/邮箱");
	}else{
		$("#find_show").html("");
	}
})
$("#find_pwd_btn").click(function(){
	$mobile = $.trim($("[name=find_username]").val());
	if(isMobile_Email($mobile) == false){
		$("#find_show").html("请输入正确的手机号/邮箱");
		$("#find_username").focus();
		return false;
	}
    $.ajax({
		type:'POST',
		cache:false,
		url:'user.php?act=get_password',
		dataType:'html',
		data:$("#retrieve_form").serialize(),
		success:function(data)
		{
			if(data == "true"){
				findpass();
			}
			else{
				$("#find_show").html("用户名/验证码错误");
				$("#image_captcha2").click();	
			}	
		},
		error:function()
		{
			$("#tips_big_div").show();
			$("#tips_word").html("提交失败,请重新提交。");
		}
	})
})
})

$(function(){
	//注册
	$("[name=register_username]").blur(function(){
		$mobile = $.trim($("[name=register_username]").val());
		var username = $("#username").val();
		if(isMobile_Email($mobile) == false){
			$("[name=register_jinggao]").html("<span class=red>手机/邮箱错误</span>");
		}else{
			$.ajax({
				type:'POST',
				cache:false,
				url:'user.php?act=is_registered',
				dataType:'html',
				data:"register_username="+username,
				success:function(data)
				{
					if(data =="true"){
						$("[name=register_jinggao]").html("<span class=red>可以注册</span>");
					}else{
						$("[name=register_jinggao]").html("<span class=red>该用户名已存在，立刻<a href='javascript:onclick=login_click()'>登录</a></span>");
					}
				}
			})
		}
	})	
	
	$("#register_btn").bind('click', function() {
		$mobile = $.trim($("[name=register_username]").val());
		var username = $("#username").val();
		if(isMobile_Email($mobile) == false){
			$("[name=register_jinggao]").html("<span class=red>手机/邮箱错误</span>");
		}else{
			$.ajax({
				type:'POST',
				cache:false,
				url:'user.php?act=is_registered',
				dataType:'html',
				data:"register_username="+username,
				success:function(data)
				{
					if(data =="true"){
						$("[name=register_jinggao]").html("<span class=red>可以注册</span>");
					}else{
						$("[name=register_jinggao]").html("<span class=red>该用户名已存在，立刻<a href='javascript:onclick=login_click()'>登录</a></span>");
					}
				}
			})
		}
	})
	$("#register_btn").click(function(){
		registerIn();
	})
})

//手机，邮箱验证
function isMobile_Email(value)   
{   
if(/^13\d{9}$/g.test(value)||(/^15[0-35-9]\d{8}$/g.test(value))||   
(/^14[0-35-9]\d{8}$/g.test(value))||(/^18[01-9]\d{8}$/g.test(value))
){     
	return true;   
}else if( /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(value)){   
	return true;   
}else{
	return false;
	}
}

function registerIn()
{
  var frm = document.forms['ECS_REGISTERFORM'];

	if (frm)
	{
		var username = frm.elements['register_username'].value;
		var password = frm.elements['register_password'].value;

		if (username.length == 0)
		{
			$("[name=register_jinggao]").html("<span class=red>手机/邮箱不能为空</span>");
			return false;
		}
		if (username.length > 30)
		{
			$("[name=register_jinggao]").html("<span class=red>邮箱长度最多为30个字符</span>");
			return false;
		}
		if (password.length < 6)
		{
			$("#gray_show").html("密码最少为6个字符");
			return false;
		}
		if (password.length > 20)
		{
			$("#gray_show").html("密码最多为20个字符");
			return false;
		}
		$mobile = $.trim($("[name=register_username]").val());
		if(isMobile_Email($mobile) == false){
			$("[name=register_jinggao]").html("<span class=red>手机/邮箱错误</span>");
			return false;
		}
		else
		{
			Ajax.call('user.php?act=act_register', 'register_username=' + username + '&register_password=' + encodeURIComponent(password) , registerResponse, "POST", "TEXT");
			
		}
	}
	else
	{
		$("#tips_big_div").show();
		$("#tips_word").html("Template error!");
	}
}

function registerResponse(result)
{
  var userName = document.forms['ECS_REGISTERFORM'].elements['register_username'].value;
  var mzone = document.getElementById("ECS_MEMBERZONE");  
  var res   = $.evalJSON(result);
	if(mzone){
		mzone.innerHTML = res.content;
		evalscript(res.ucdata);
		$('#formUser_reg')[0].reset();
		$("#bg").hide();
		$("#login_main").hide();
		$('#apc').attr('href','user.php'); 
		//wwzedit				
		if(window.location.pathname === "/flow.php"){
			window.location.reload();
			if(window.location.search === "?step=cart" && $("#flowcartCheckLogin").val() === "1" ){
				window.location.href = "/flow.php?step=checkout" ;
			}
		}
	}else
	{
		$("#tips_big_div").show();
		$("#tips_word").html("Template error!");
	}
}

$(function(){
	//登陆
	$("[name=username]").blur(function(){ 								
		$mobile = $.trim($("[name=username]").val());
		if(isMobile_Email($mobile) == false){
			$("#login_jinggao").show().html("请输入正确的手机号/邮箱");
		}else{
			$("#login_jinggao").show().html("");
		}
	})
	$("#login_btn").click(function(){
		signIn();
	})
})

function signIn()
{
	var frm = document.forms['ECS_LOGINFORM'];

	if (frm)
	{
		var username = frm.elements['username'].value;
		var password = frm.elements['password'].value;
		var captcha = '';
		if (frm.elements['captcha'])
		{
			captcha = frm.elements['captcha'].value;
		}

		if (username.length == 0)
		{
			$("#login_jinggao").show().html("手机号/邮箱不能为空");
			return false;
		}
		if (password.length < 6)
		{
			$("#login_jinggao").show().html("密码最少为6个字符");
			return false;
		}
		$mobile = $.trim($("[name=username]").val());
		if(isMobile_Email($mobile) == false){
			$("#login_jinggao").show().html("请输入正确的手机号/邮箱");
			return false;
		}
		else
		{
			Ajax.call('user.php?act=signin', 'username=' + username + '&password=' + encodeURIComponent(password) + '&captcha=' + captcha, signinResponse, "POST", "TEXT");
		}
	}
	else
	{
		$("#tips_big_div").show();
		$("#tips_word").html("Template error!");
	}
}

function signinResponse(result)
{
	var userName = document.forms['ECS_LOGINFORM'].elements['username'].value;
	var mzone = document.getElementById("ECS_MEMBERZONE");  
	var res   = $.evalJSON(result);
	
	document.cookie="loginfail = " + res.login_fail; 
	
	if(res.login_fail > 1){
		$("#image_captcha").click();
		$("#login_captcha").show();
	}
	
	if (res.error == 1 || res.error == 2)
	{
		// 登录失败
		$("#login_jinggao").show().html("用户名或密码错误");
	}else if(res.error == 3)
	{
		// 登录失败
		$("#image_captcha").click();
		$("#login_jinggao").show().html("验证码错误");
	}
	
	else
	{
		if (mzone)
		{
			if(res.content == "false"){
				$("#login_jinggao").show().html("验证码错误");
			}else{
				mzone.innerHTML = res.content;
				evalscript(res.ucdata);
				$('#ECS_LOGINFORM')[0].reset();
				$("#bg").hide();
				$("#login_main").hide();
				$('#apc').attr('href','user.php');
				//wwzedit				
				if(window.location.pathname === "/flow.php"){
					window.location.reload();
					if(window.location.search === "?step=cart" && $("#flowcartCheckLogin").val() === "1" ){
						window.location.href = "/flow.php?step=checkout" ;
					}
				}
			}
		}
		else
		{
			$("#tips_big_div").show();
			$("#tips_word").html("登陆失败。");
		}
	}
}

/* 会员登出 */
function signout()
{
	Ajax.call('user.php?act=signout','',signoutResponse,"GET","TEXT");
}
function signoutResponse(result)
{
	/*
	var res   = $.evalJSON(result);
	var mzone = document.getElementById("ECS_MEMBERZONE");
	mzone.innerHTML = res.content;
	$("#login_main").hide();
	$("#bg").remove();
	$("#login_captcha").hide();
	*/
	document.cookie="loginfail = 0"; 
	location.href = '/';
}

$(function(){
	var fe_1 = $("#fe_1").val();
	var fe_2 = $("#fe_2").val();
	var fe_3 = $("#fe_3").val();
	var fe_4 = fe_1 + fe_2;
	
	if(fe_3 > fe_4){
		$("#inv_fee").html("10.00");
	}else{
		$("#inv_fee").html("0.00");
	}
})

$(function(){
	//登陆回车事件
	$("#keyloginpsd").keydown(function(e){ 
	var keyloginpsd = e.which; 
	if(keyloginpsd == 13){ 
	$("#login_btn").click(); 
	return false; 
	} 
	}); 
	
	//登陆回车事件
	$("#keylogincaptcha").keydown(function(e){ 
	var keylogincaptcha = e.which; 
	if(keylogincaptcha == 13){ 
	$("#login_btn").click(); 
	return false; 
	} 
	});

	//注册回车事件
	$("[name=register_password]").keydown(function(e){ 
	var keyregisterpsd = e.which; 
	if(keyregisterpsd == 13){ 
	$("#register_btn").click(); 
	return false; 
	} 
	}); 
	
	//注册回车事件
	$("#captcha").keydown(function(e){ 
	var captcha = e.which; 
	if(captcha == 13){ 
	$("#find_pwd_btn").click(); 
	return false; 
	} 
	}); 
	
	
	var strCookie=document.cookie; 
	var arrCookie=strCookie.split("; "); 
	var loginfailnum;
	for(var i=0;i<arrCookie.length;i++){ 
	var arr=arrCookie[i].split("="); 
	//找到名称为loginfail = 3(次数)分成数组array(0=>'loginfail = ',1=>'3'); 
	if("loginfail"==arr[0]){ 
	loginfailnum=arr[1]; 
	break; 
	} 
	} 
	if(loginfailnum > 1){
		$("#image_captcha").click();
		$("#login_captcha").show();
	}
 
})

//手机验证
function isMobelphone(value)   
{   
	if( /^13\d{9}$/g.test(value)||(/^15[0-35-9]\d{8}$/g.test(value))|| (/^14[0-35-9]\d{8}$/g.test(value))||(/^18[01-9]\d{8}$/g.test(value)) )
	{     
		return true;   
	}else{
		return false;
	}
}







